package com.pinfly.common.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * This class is intended for use in Logger.entering statements to hide actual
 * passwords and passcodes.
 */
public class CleanUtil
{

    private static final String[] s_words =
    { "passcode", "password", "passphrase" };
    private static final String s_mask = "*****";

    /**
     * Returns a boolean indicating that the specified name is a protected name
     * or not. If name is empty or null, false is returned.
     * 
     * @param name
     * @return True if protected
     */
    public static boolean containsProtected (String name)
    {
        if (StringUtil.isNullOrBlank (name))
        {
            return false;
        }

        for (int x = 0; x < s_words.length; x++)
        {
            if (name.toUpperCase ().contains (s_words[x].toUpperCase ()))
            {
                return true;
            }
        }

        return false;
    }

    /**
     * Hides the value if the name contains a one of the protected words.
     * 
     * @param name
     * @param value
     * @return a cleaned value
     */
    public static String cleanString (String name, String value)
    {
        if (name == null || value == null)
        {
            return value;
        }

        if (CleanUtil.containsProtected (name))
        {
            return s_mask;
        }
        return value;
    }

    /**
     * Returns a properties object with cleaned values, if the names contain the
     * masked words (password or passcode). If props is null or empty it is
     * returned.
     * 
     * @param props
     * @return clean properties
     */
    public static Properties cleanProp (Properties props)
    {

        if (props == null || props.size () == 0)
        {
            return props;
        }

        Properties newProp = new Properties ();
        for (Map.Entry <Object, Object> entry : props.entrySet ())
        {
            String name = (String) entry.getKey ();
            String value = (String) entry.getValue ();
            if (CleanUtil.containsProtected (name))
            {
                value = s_mask;
            }

            newProp.put (name, value);

        }

        return newProp;
    }

    /**
     * Returns a Map object with cleaned values, if the names contain the masked
     * words (password or passcode). If map is null or empty it is returned.
     * 
     * @param map
     * @return Cleaned Map
     */
    public static Map <String, String> cleanMap (Map <String, String> map)
    {
        if (map == null || map.size () == 0)
        {
            return map;
        }

        Map <String, String> newMap = new HashMap <String, String> ();
        for (Map.Entry <String, String> entry : map.entrySet ())
        {
            String name = (String) entry.getKey ();
            String value = (String) entry.getValue ();
            if (CleanUtil.containsProtected (name))
            {
                value = s_mask;
            }

            newMap.put (name, value);
        }
        return newMap;
    }

    // public static void main (String[] args)
    // {
    // Properties p = new Properties();
    // p.put ("tester.passcode", "12345");
    // p.put ("tester.name", "kevin");
    // p.put ("tester.Password", "232422");
    // p.put ("tester.Passcode", "WSEDDEWSSQAQA");
    // p.put ("tester.Passcode1", "WSEDDEWSSQAQA");
    // p.put ("tester.Passcode222", "WSEDDEWSSQAQA");
    // p.put ("tester.222Passcode1", "SSQAQA");
    //
    // System.out.println(cleanProp(p));
    // }

}
